<template>
  <!-- category（分类）信息管理页 -->
  <div class="main">
    <!-- 分类信息列表渲染 -->
    <el-table
      :data="stores"
      style="width: 1002px; margin: 0 auto"
      max-height="352"
      stripe
      border
      :header-cell-style="headerStyle"
    >
      <!-- ID项 -->
      <el-table-column prop="storeId" label="ID" width="250"> </el-table-column>
      <!-- 名称项 -->
      <el-table-column prop="storeName" label="名称" width="250">
      </el-table-column>
      <!-- 开店时间 -->
      <el-table-column prop="storeStartTime" label="创建时间" width="250">
      </el-table-column>
      <!-- 操作栏 -->
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            type="primary"
            @click="openEditButton(scope.$index, stores)"
            icon="el-icon-edit"
            size="small"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            @click="deleteRow(scope.$index, stores)"
            size="small"
            type="primary"
            icon="el-icon-delete"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加店铺按钮 -->
    <el-button
      @click="addDialogFlag = true"
      class="addButton"
      size="medium"
      type="primary"
      round
      style="margin-bottom: 10px"
      >添加店铺</el-button
    >
    <!-- 添加提示输入名称dialog -->
    <el-dialog
      title="请输入要添加的店铺名称："
      :visible.sync="addDialogFlag"
      width="30%"
      center
      top="30vh"
    >
      <el-input v-model="storeNameWillBeAdd" size="small"> </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="addStore()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑店铺信息dialog -->
    <el-dialog
      :visible.sync="editDialogFlag"
      width="50%"
      center
      top="10vh"
      class="storeEdit"
    >
      <el-form
        label-position="right"
        size="middle"
        style="font-weight: 800; font-size: 16px"
      >
        <el-form-item label="ID：" label-width="100px">
          {{storeIdEdit}}
        </el-form-item>
        <el-form-item label="店铺名称：" label-width="100px">
          <el-input v-model="storeNameEdit" size="small"> </el-input>
        </el-form-item>
        <el-form-item label="头像：" label-width="100px">
          <el-upload
            :action="''"
            :show-file-list="false"
            :limit="1"
            :auto-upload="false"
            accept="image/jpeg,image/jpg,image/png"
            :on-change="handleAvatarChange"
          >
            <div>
              <el-image
                style="width: 100px; height: 100px"
                :src="imgUrl === '' ? storeImageEdit : imgUrl"
                fit="cover"
              ></el-image>
              <div v-show="imgUrl === ''">
                <el-button size="small" type="primary">点击上传</el-button>
              </div>
            </div>
            <div v-show="imgUrl === ''" slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过1mb
            </div>
            <div
              v-show="imgUrl != ''"
              slot="tip"
              class="el-upload__tip"
              style="color: green"
            >
              上传成功！
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="editStore">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  inject:['reload'],
  data() {
    return {
      uploadUrl: this.$url + "jw-store/modify",
      stores: [],
      isInputName: false,
      addDialogFlag: false,
      editDialogFlag: false,
      storeNameEdit: "",
      storeIdEdit: "",
      storeImageEdit: "",
      imgUrl: "",
      uploadFile: "",
      type: "",
      storeNameWillBeAdd: "",
      row: {},
    };
  },
  mounted() {
    this.getStores();
  },
  methods: {
    headerStyle() {
      return "color: #000;background-color: #c5464a;height:60px;";
    },
    // 获取所有店铺信息
    getStores() {
      let _this = this;
      this.$http.get("jw-store/list").then((response) => {
        _this.stores = response.data.result;
      });
    },
    // 删除选择行
    deleteRow(index, data) {
      const id = data[index].storeId;
      const name = data[index].storeName;
      this.$confirm(
        "此操作将永久删除ID为【" +
          id +
          "】，名称为【" +
          name +
          "】的项, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          data.splice(index, 1);
          this.stores = data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 删除数据库中的对应项
          let json = {};
          json.id = id;
          this.$http
            .post("jw-store/delete", json)
            .then((response) => {
              this.getStores();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑店铺
    editStore(row) {
      let param = {};
      param.file = this.imgUrl;
      param.id = this.row.storeId;
      param.name = this.storeNameEdit;
      param.mode = this.type;
      this.$http
        .post("jw-store/modify", param)
        .then((res) => {})
        .catch((err) => {
          console.log("failed");
        });
      this.editDialogFlag = false;
      this.imgUrl = "";
      this.getStores();
      this.$message({
        type: "success",
        message: "修改成功",
      });
      this.reload();
    },
    // 编辑店铺信息
    openEditButton(index, stores) {
      this.editDialogFlag = true;
      this.storeNameEdit = stores[index].storeName;
      this.storeIdEdit = stores[index].storeId;
      this.storeImageEdit = stores[index].storeImage;
      this.row = stores[index];
    },
    // 上传图片
    handleAvatarChange(file) {
      console.log("1");
      const isIMAGE =
        file.raw.type === "image/jpeg" || file.raw.type === "image/png";
      const isLt1M = file.size / 1024 / 1024 < 1;
      let _this = this;
      this.uploadFile = file;
      this.type = this.uploadFile.name.substring(
        this.uploadFile.name.lastIndexOf(".")
      );
      if (!isIMAGE) {
        this.$message.error("上传文件只能是图片格式!");
        this.reload();
        return false;
      }
      if (!isLt1M) {
        this.$message.error("上传文件大小不能超过 1MB!");
        this.reload();
        return false;
      }
      var reader = new FileReader();
      reader.readAsDataURL(file.raw);
      reader.onload = function (e) {
        _this.imgUrl = this.result;
      };
    },
    addStore() {
      this.addDialogFlag = false;
      let store = {};
      store.storeName = this.storeNameWillBeAdd;
      this.$http.post("jw-store/add", store).then((reponse) => {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        // 重新获取列表数据
        this.getStores();
      });
    },
  },
};
</script>

<style scope>
.main {
  overflow: hidden;
}
.el-input__inner {
  padding: 0 !important;
}
.addButton {
  margin-top: 12px !important;
  float: right;
  right: 30px;
}
.storeEdit .el-input {
  margin-top: 5px;
  width: 300px;
}
</style>
